<?php

namespace app\admin\model\shareholder;

use app\admin\model\BaseMode;
use app\admin\model\Store;
use think\exception\ValidateException;

class ShareholderModel extends BaseMode
{

    protected $table='p_shareholder';

    public static function getList(){

        $where=[];
        $store_json=session('store_id');
        $where[]=['store_id','in',$store_json];
        return  (new ShareholderModel)->where($where)->page(input('page',1))->limit(input('limit',20))->select();
    }


    public static function insertShareholder($add){
        try {
              $info=getBaseUsers($add['mobile']);
              if(!$info){
                  return array(
                      'code' => 400,
                      'msg' => '手机号填写错误!',
                  );

             }
             $infos=self::where(['store_id'=>session('store_id'),'mobile'=>$add['mobile']])->find();
              if($infos){
                  return array(
                      'code' => 400,
                      'msg' => '股东已经存在!',
                  );
              }

              $add['store_id']=session('store_id');
              $add['mobile']=$info['mobile'];
              $add['uuid']=$info['uuid'];
              self::create($add);
            return array(
                'code' => 200,
                'msg' => '成功!',
                'url'=>url('/Shareholder/ShareholderList')->build()
            );

            // dump($result);die;

        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }

    }

    public static function modifyShareholder($add)
    {
        try {
            $result = self::where(['id' => input('id')])->save($add);

            if (!empty($result)) {
                return array(
                    'code' => 200,
                    'msg' => '成功!',
                    'url' => url('/Shareholder/ShareholderList')->build()
                );
            } else {
                return array(
                    'code' => 40002,
                    'msg' => '成功!',
                );
            }

        } catch (ValidateException $e) {
            // 验证失败 输出错误信息

            return $e->getError();
        }
    }

}